#include "../header/common.h" 
#define MAXSCORES 10

int highscore(int rw,SDL_Surface *screen,char playername[],int points) 
{
  FILE *dateilink;
  int i=0,kickme=15,fonthigh,ende=0,x=0,y=0;
  SDL_Rect backbutton;
  SDL_Surface *highscores,*backbuttons,*backbuttonspressed;
  SDL_Event event;
  TTF_Font *font,*font2; 
  char pointstring[200];
  struct eintragsart
  {
    char name[15];
    int point;
  }eintrag[MAXSCORES];
  Uint8 SDL_GetRelativeMouseState(int *x, int *y);
   
  backbutton.x=screen->w/5;
  backbutton.y=((screen->h/4)*3);

if (TTF_Init() == -1) 
{
  printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError());

}

  switch(screen->w)
    {
    case 640:
      backbuttons = SDL_LoadBMP("image/640480/buttons/option5.bmp");
      backbuttonspressed = SDL_LoadBMP("image/640480/buttonspressed/aoption5.bmp");
      highscores = SDL_LoadBMP("image/640480/highscore.bmp");
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 23);
      font2 = TTF_OpenFont("spranq_eco_sans_regular.ttf", 50);
      fonthigh=23;
      break;
    case 800:
      backbuttons = SDL_LoadBMP("image/800600/buttons/option5.bmp");
      backbuttonspressed = SDL_LoadBMP("image/800600/buttonspressed/aoption5.bmp");
      highscores = SDL_LoadBMP("image/800600/highscore.bmp");
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 28);
      font2 = TTF_OpenFont("spranq_eco_sans_regular.ttf", 70);
      fonthigh=28;
      break;
    case 1024:
      backbuttons = SDL_LoadBMP("image/1024768/buttons/option5.bmp");
      backbuttonspressed = SDL_LoadBMP("image/1024768/buttonspressed/aoption5.bmp");
      highscores = SDL_LoadBMP("image/1024768/highscore.bmp");
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 36);
      font2 = TTF_OpenFont("spranq_eco_sans_regular.ttf", 90);
      fonthigh=36;
      break;
    }


  SDL_SetColorKey(highscores, SDL_SRCCOLORKEY, SDL_MapRGB(highscores->format, 255, 255, 255)); 
  SDL_SetColorKey(backbuttons, SDL_SRCCOLORKEY, SDL_MapRGB(highscores->format, 0, 0, 255)); 
  SDL_SetColorKey(backbuttonspressed, SDL_SRCCOLORKEY, SDL_MapRGB(highscores->format, 0, 0, 255)); 
  backbutton.w=backbuttons->w;
  backbutton.h=backbuttons->h;

  dateilink = fopen ("high.score","rb");
  while(((fscanf(dateilink,"%d:%15s\n",&eintrag[i].point,&eintrag[i].name[0])) != EOF )||(i==MAXSCORES))i++;
  fclose(dateilink);
  
  switch(rw)
    {
    case 0:
      for(i=0;i<MAXSCORES;i++)
	{
	  sprintf(pointstring,"%d Points",eintrag[i].point);  
 	  drawText(highscores,highscores,font,(eintrag[i].name),(screen->w/4),((i*fonthigh)+(screen->h/4)),255,255,255);  
    	  drawText(highscores,highscores,font,pointstring,screen->w/2,((i*fonthigh)+(screen->h/4)),255,255,255);  
	}
      SDL_BlitSurface(highscores, NULL, screen, NULL); 
      SDL_BlitSurface(backbuttons,NULL,screen,&backbutton);
      SDL_Flip(screen); 

     while (!ende) 
	{
	  while (SDL_PollEvent(&event)) 
	    {
	      SDL_PumpEvents();
	      if(SDL_GetMouseState(&x, &y) &SDL_BUTTON(1)) 
		{
		  if((x>backbutton.x)&&(x<backbutton.x+backbutton.w)&&(y>backbutton.y)&&(y<backbutton.y+backbutton.w))
		    {
 		      SDL_FreeSurface(highscores); 
 		      SDL_FreeSurface(backbuttons); 
 		      SDL_FreeSurface(backbuttonspressed); 
		      return 0;
		    }
		}
	      if(!ende)
		{
		  SDL_Delay(15);
		  if(click_button(backbutton,x,y))
		    {
		      SDL_BlitSurface(backbuttonspressed, NULL, screen, &backbutton);
		    }
		  else
		    {
		      SDL_BlitSurface(backbuttons,NULL,screen,&backbutton);
		    }
		  SDL_Flip(screen);
		}
	      switch (event.type) 
		{
		case SDL_QUIT:
		  SDL_FreeSurface(highscores);
		  SDL_FreeSurface(backbuttons);
		  SDL_FreeSurface(backbuttonspressed);
		  return 1;
		}
	    }
	}
      break;
    case 1:
      for(i=0;i<MAXSCORES;i++)if(points>eintrag[i].point){kickme=i;break;}
      if(kickme!=15)
	{
	  for(i=MAXSCORES-1;i>kickme;i--)
	    {
	      eintrag[i].point=eintrag[i-1].point;
	      sprintf(eintrag[i].name,"%s",eintrag[i-1].name);
	    }
	  eintrag[kickme].point=points;
	  sprintf(eintrag[kickme].name,"%s",playername);
	  
	  dateilink = fopen ("high.score","wb");
	  for(i=0;i<MAXSCORES;i++)
	    {
	      fprintf(dateilink,"%d:%s\n",(eintrag[i].point),(eintrag[i].name));
	    }	      
	  fclose(dateilink); 
	  return 2;
	}
      break;
    }
  return 0;
}
